package cn.tedu.zx.product.webapi.service.iml;


import cn.tedu.zx.pojo.court.dto.AlbumAddNewDto;
import cn.tedu.zx.pojo.court.dto.AlbumUpdateDto;
import cn.tedu.zx.pojo.court.dto.PictureAddNewDto;
import cn.tedu.zx.pojo.court.dto.PictureUpdateDto;
import cn.tedu.zx.pojo.court.entity.Album;
import cn.tedu.zx.pojo.court.entity.Picture;
import cn.tedu.zx.pojo.court.vo.*;
import cn.tedu.zx.product.service.AlbumService;
import cn.tedu.zx.product.service.PictureService;
import cn.tedu.zx.product.webapi.ex.ServiceCode;
import cn.tedu.zx.product.webapi.ex.ServiceException;
import cn.tedu.zx.product.webapi.mapper.AlbumMapper;
import cn.tedu.zx.product.webapi.mapper.PictureMapper;
import cn.tedu.zx.product.webapi.util.PageInfoToPageDataConverter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class PictureServiceIml implements PictureService {
    private String message = null;

    @Autowired
    private PictureMapper pictureMapper;

    @Override
    public void addNew(PictureAddNewDto pictureAddNewDto) {
        log.debug("往相册添加文件");
        QueryWrapper<Picture> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", pictureAddNewDto.getUrl());/*获取传入的名称*/
        int countByUrl = pictureMapper.selectCount(queryWrapper);
        log.debug("查询数据{}",countByUrl);
        if (countByUrl>0){
            message = "添加失败，相册名已占用";
            log.warn(message);
//            System.out.println(message);
            throw new ServiceException(ServiceCode.ERR_OCCUPY,message);
        }
        Picture picture = new Picture();
        BeanUtils.copyProperties(pictureAddNewDto,picture);
        picture.setCreated(LocalDateTime.now());
        picture.setUpdated(LocalDateTime.now());
        log.trace("往数据库添加子项：{}",picture);
        int insert = pictureMapper.insert(picture);
        if (insert != 1){
            String message = "服务器忙,m请稍后再试";
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
        log.trace("{}添加完成",picture);
    }

    @Override
    public void deleteById(Long id) {
        log.debug("{}",id);
        QueryWrapper<Picture> pictureWrapper = new QueryWrapper();
        pictureWrapper.eq("id",id);
        int countById = pictureMapper.selectCount(pictureWrapper);
        if (countById==0){
            message = "删除相册失败，相册不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOTFOUND,message);
        }

        int delete = pictureMapper.deleteById(id);
        if (delete != 1){
            String message = "服务器忙,m请稍后再试";
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }
    @Override
    public void updateById(PictureUpdateDto pictureUpdateDto) {
        log.debug("{}",pictureUpdateDto);
        QueryWrapper<Picture> pictureWrapper = new QueryWrapper();
        pictureWrapper.eq("name",pictureUpdateDto.getUrl()).ne("id",pictureUpdateDto.getId());
        int countById = pictureMapper.selectCount(pictureWrapper);
        if (countById>0){
            message = "修改失败，相册名已占用";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_OCCUPY,message);
        }
        Picture picture = new Picture();
        BeanUtils.copyProperties(pictureUpdateDto,picture);
        picture.setUpdated(LocalDateTime.now());
        log.trace("往数据库修改子项：{}",picture);
        int updateById = pictureMapper.updateById(picture);
        if (updateById != 1){
            String message = "服务器忙,m请稍后再试";
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
        log.trace("{}修改完成",picture);
    }

    @Override
    public PictureDetailsVo selectDetailsById(Long id) {
        log.debug("传入的id：{}",id);
        PictureDetailsVo pictureDetailsVo = pictureMapper.selectDetailsById(id);
        log.debug("查到的数据：{}",pictureDetailsVo);
        if (pictureDetailsVo==null){
            message = "查询失败，根据【id="+id+"】查询的相册详情不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOTFOUND,message);
        }
        return pictureDetailsVo;
    }

    @Override
    public PageData list(Integer pageNum) {
        Integer pageSize = 5;
        return list(pageNum,pageSize);
    }

    @Override
    public PageData<PictureListVo> list(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<PictureListVo> list = pictureMapper.list();
        PageInfo pageInfo = new PageInfo(list);
        return PageInfoToPageDataConverter.converter(pageInfo);
    }


}
